﻿using System;
using System.Collections.Generic;
using System.Linq;
using Me.Prettyprint.Hector.Api.Beans;

namespace Me.Prettyprint.Cassandra.Utils
{
    /// <summary>
    /// Since the default IEnumerator in .Net doesn't have an easy way to peek at
    /// the next element in a list, this class was created to make it easier.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class PeekEnumerator<K,N,V>
    {
        public PeekEnumerator(List<Me.Prettyprint.Hector.Api.Beans.Row<K, N, V>> list)
        {
            Rows = list;
            // we start with -1, so that first time we call MoveNext() it is at zero
            Position = -1;
        }
        private int Position
        {
            get;
            set;
        }
        private List<Me.Prettyprint.Hector.Api.Beans.Row<K, N, V>> Rows
        {
            get;
            set;
        }
        public Me.Prettyprint.Hector.Api.Beans.Row<K, N, V> Current
        {
            get
            {
                if (Position > -1)
                {
                    return Rows[Position];
                }
                else
                {
                    return null;
                }
            }
        }
        public bool MoveNext()
        {
            if ((Position + 1) < Rows.Count)
            {
                Position = Position + 1;
                return true;
            }
            else
            {
                return false;
            }
        }
        public Me.Prettyprint.Hector.Api.Beans.Row<K, N, V> Peek()
        {
            if ((Position + 1) < Rows.Count)
            {
                return Rows[Position + 1];
            }
            else
            {
                return null;
            }
        }
        public bool HasNext()
        {
            return (Position + 1) < Rows.Count;
        }
        public void Reset()
        {
            Position = -1;
        }
    }
}
